package com.futurelight.recipes.model;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.futurelight.recipes.dao.EntityFavorite;
import com.futurelight.recipes.dao.EntityResult;
import com.futurelight.recipes.repository.local.LocalRecipesRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FavoriteViewModel extends AndroidViewModel {
    private LocalRecipesRepository localRecipesRepository;

    private MutableLiveData<List<Result>> liveData;
    private MutableLiveData<List<String>> candidates;
    private MutableLiveData<Boolean> favorite;
    SharedPreferences sharedPreferences = getApplication().getSharedPreferences(getApplication().getPackageName(), Context.MODE_PRIVATE);

    /**
     * 食谱列表LiveData
     * @return
     */
    public MutableLiveData<List<Result>> getData() {
        if (liveData == null) {
            liveData = new MutableLiveData<>();
        }
        return liveData;
    }

    /**
     * 搜索历史记录候选词LiveData
     * @return
     */
    public MutableLiveData<List<String>> getCandidates() {
        if (candidates == null) {
            candidates = new MutableLiveData<>();
        }
        return candidates;
    }


    /**
     * 读取数据库判断是否是喜欢
     * @return
     */
    public MutableLiveData<Boolean> isFavorite() {
        if (favorite == null) {
            favorite = new MutableLiveData<>();
        }
        return favorite;
    }


    public FavoriteViewModel(@NonNull Application application) {
        super(application);
        localRecipesRepository = LocalRecipesRepository.getInstance(getApplication());
    }

    /**
     * 异步获取喜欢列表
     */
    public void getFavoriteResults() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<EntityResult> favorites = localRecipesRepository.getFavorites();
                List<Result> results = new ArrayList<>(favorites.size());
                for (EntityResult entityResult : favorites) {
                    results.add(entityResult.result);
                }
                liveData.postValue(results);
            }
        }).start();
    }

    /**
     * 查找id，在喜欢列表数据库中是否存在
     * @param id
     */
    public void isFavorite(int id) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                EntityFavorite entityFavorite = localRecipesRepository.findFavorite(id);
                if (entityFavorite == null || entityFavorite.result_id != id) {
                    favorite.postValue(false);
                } else {
                    favorite.postValue(true);
                }
            }
        }).start();
    }

    /**
     * 插入一条喜欢食谱
     * @param entityFavorite
     */
    public void insertFavorite(EntityFavorite... entityFavorite) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                localRecipesRepository.insertFavorite(entityFavorite);
            }
        }).start();
    }

    /**
     * 删除一条喜欢食谱
     * @param entityFavorite
     */
    public void deleteFavorite(EntityFavorite... entityFavorite) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                localRecipesRepository.deleteFavorite(entityFavorite);
            }
        }).start();
    }


    public void search(String query) {

    }

}
